package cn.qlu.yhy.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.qlu.yhy.model.Book;
import cn.qlu.yhy.model.Page;
import cn.qlu.yhy.model.User;
import cn.qlu.yhy.service.IBookService;

@Controller
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    private IBookService bookService;

    // select one book by book id
    @ResponseBody
    @RequestMapping(value = "findPageAboutBook", method = RequestMethod.GET)
    public Page<Book> findPageAboutBook(Page<Book> page, HttpSession session) throws ServletException, IOException {
    	System.out.println("--------" + page);
    	if (page == null) {
    		page = new Page("10", "1");
    	}
    	User user = (User) session.getAttribute("user");
    	page.setList(bookService.findBooksAllByUserId(user.getId(), page));

        return page;
    }


    // dispatcher my book and show book list
    @ResponseBody
    @RequestMapping(value = "myBook", method = RequestMethod.GET)
    public ModelAndView toShowMyBook(HttpSession session) {

    	ModelAndView modelAndView = null;
    	try {
    		modelAndView = new ModelAndView();
			modelAndView.addObject("bookList", findPageAboutBook(new Page<Book>("10", "1"), session).getList());
			modelAndView.setViewName("userbook/my_book");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	

        return modelAndView;
    }


    // select one book by book id
    @ResponseBody
    @RequestMapping(value = "selectMyBookByBookId", method = RequestMethod.GET)
    public ModelAndView selectMyBookByBookId(int bookId) throws ServletException, IOException {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bookList", bookService.getBookByBookId(bookId));

        return modelAndView;
    }


    // select book list by book name/ author/ press fuzzy query
    @ResponseBody
    @RequestMapping(value = "selectBooksByBookNAP", method = RequestMethod.GET)
    public Page selectMyBookByBookId(String bookNAP, Page page) throws ServletException, IOException {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bookList", bookService.findBooksByBookNAP(bookNAP));

        return page;
    }

    // create book
    @ResponseBody
    @RequestMapping(value = "shareBook", method = RequestMethod.POST)
    public ModelAndView create(Book book) throws ServletException, IOException {

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("----------------tests");
        if (bookService.createBook(book)) {
            modelAndView.setViewName("user_home");
        } else {
            modelAndView.setViewName("shareBook");
        }
        //        boolean flag = questionService.createQuestion(questionC);


        //        modelAndView.addObject("questionId", questionC.getQuestionId());
        //        modelAndView.addObject("showConfirm", flag);

        return modelAndView;
    }


    // delete book
    @ResponseBody
    @RequestMapping(value = "deleteMyBook", method = RequestMethod.GET)
    public ModelAndView selectMyBook(int bookId) throws ServletException, IOException {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("deleteFlag", bookService.deleteBook(bookId));

        return modelAndView;
    }


    // update book
    @ResponseBody
    @RequestMapping(value = "updateMyBook", method = RequestMethod.GET)
    public ModelAndView selectMyBook(Book book) throws ServletException, IOException {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("updateFlag", bookService.updateBook(book));

        return modelAndView;
    }



}